<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>嵌套对象映射 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-mapping.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-mapping.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="nested-objects.html">嵌套对象</a></span>
»
<span class="breadcrumb-node">嵌套对象映射</span>
</div>
<div class="navheader">
<span class="prev">
<a href="nested-objects.html">« 嵌套对象</a>
</span>
<span class="next">
<a href="nested-query.html">嵌套对象查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="nested-mapping"></a>嵌套对象映射 (Nested Object Mapping)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/402_Nested/31_Nested_mapping.asciidoc">edit</a>
</h2>
</div></div></div>
<p>设置一个字段为 <code class="literal">nested</code> 很简单 —  你只需要将字段类型 <code class="literal">object</code> 替换为 <code class="literal">nested</code> 即可：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /my_index
{
  "mappings": {
    "blogpost": {
      "properties": {
        "comments": {
          "type": "nested", <a id="CO270-1"></a><i class="conum" data-value="1"></i>
          "properties": {
            "name":    { "type": "string"  },
            "comment": { "type": "string"  },
            "age":     { "type": "short"   },
            "stars":   { "type": "short"   },
            "date":    { "type": "date"    }
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO270-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">nested</code> 字段类型的设置参数与 <code class="literal">object</code> 相同。</p>
</td>
</tr>
</table>
</div>
<p>这就是需要设置的一切。至此，所有 <code class="word">comments</code> 对象会被索引在独立的嵌套文档中。可以查看 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/nested.html" class="ulink" target="_top"><code class="literal">nested</code> 类型参考文档</a> 获取更多详细信息。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="nested-objects.html">« 嵌套对象</a>
</span>
<span class="next">
<a href="nested-query.html">嵌套对象查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>